Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
async-limiter
Advanced tools
The async-limiter package is a module that allows you to limit the number of asynchronous operations that are being processed at the same time. This is useful for controlling resource usage and managing load under high-concurrency situations.
Concurrency Limiting
This code sample demonstrates how to use async-limiter to limit the number of concurrent asynchronous tasks. In this example, the concurrency limit is set to 2, meaning only two tasks will be processed at a time.
const Limiter = require('async-limiter');
const t = new Limiter({ concurrency: 2 });
for (let i = 0; i < 10; i++) {
t.push((cb) => {
setTimeout(() => {
console.log('Processed task', i);
cb();
}, 100);
});
}
t.onDone(() => {
console.log('All tasks processed');
});
Bottleneck is a powerful rate limiter that allows you to throttle functions and prioritize job queues. It provides more advanced features like clustering support and job scheduling compared to async-limiter.
p-limit is a package that limits the number of promises running at the same time. It is similar to async-limiter but works specifically with promises, providing a simple API to control concurrency.
Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. It offers more than just limiting, including a wide range of functions for control flow, collection processing, and utilities. It's more comprehensive than async-limiter.
A module for limiting concurrent asynchronous actions in flight. Forked from queue.
This module exports a class Limiter
that implements some of the Array
API.
Pass async functions (ones that accept a callback or return a promise) to an instance's additive array methods.
Certain functions, like zlib
, have undesirable behavior when
run at infinite concurrency.
In this case, it is actually faster, and takes far less memory, to limit concurrency.
This module should do the absolute minimum work necessary to queue up functions. PRs are welcome that would make this module faster or lighter, but new functionality is not desired.
Style should confirm to nodejs/node style.
var Limiter = require('async-limiter')
var t = new Limiter({concurrency: 2});
var results = []
// add jobs using the familiar Array API
t.push(function (cb) {
results.push('two')
cb()
})
t.push(
function (cb) {
results.push('four')
cb()
},
function (cb) {
results.push('five')
cb()
}
)
t.unshift(function (cb) {
results.push('one')
cb()
})
t.splice(2, 0, function (cb) {
results.push('three')
cb()
})
// Jobs run automatically. If you want a callback when all are done,
// call 'onDone()'.
t.onDone(function () {
console.log('all done:', results)
})
const zlib = require('zlib');
const Limiter = require('async-limiter');
const message = {some: "data"};
const payload = new Buffer(JSON.stringify(message));
// Try with different concurrency values to see how this actually
// slows significantly with higher concurrency!
//
// 5: 1398.607ms
// 10: 1375.668ms
// Infinity: 4423.300ms
//
const t = new Limiter({concurrency: 5});
function deflate(payload, cb) {
t.push(function(done) {
zlib.deflate(payload, function(err, buffer) {
done();
cb(err, buffer);
});
});
}
console.time('deflate');
for(let i = 0; i < 30000; ++i) {
deflate(payload, function (err, buffer) {});
}
t.onDone(function() {
console.timeEnd('deflate');
});
npm install async-limiter
npm test
var t = new Limiter([opts])
Constructor. opts
may contain inital values for:
t.concurrency
t.onDone(fn)
fn
will be called once and only once, when the queue is empty.
Array
Mozilla has docs on how these methods work here.
t.push(element1, ..., elementN)
t.unshift(element1, ..., elementN)
t.splice(index , howMany[, element1[, ...[, elementN]]])
t.concurrency
Max number of jobs the queue should process concurrently, defaults to Infinity
.
t.length
Jobs pending + jobs to process (readonly).
v1.0.1 (2019-08-02)
coverage
folder to npmignore for smaller bundleFAQs
asynchronous function queue with adjustable concurrency
The npm package async-limiter receives a total of 6,649,556 weekly downloads. As such, async-limiter popularity was classified as popular.
We found that async-limiter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.